package com.dmgn.serviceimpl;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dmgn.bean.User;
import com.dmgn.dao.UserMapper;
import com.dmgn.service.IUserService;
import com.dmgn.util.Setting;

public class UserServiceImpl implements IUserService{
	
	private ApplicationContext context;
	private int pageItem = Setting.pageItem;
	
	UserMapper userMapper;
	public UserServiceImpl(){
		context=new ClassPathXmlApplicationContext("applicationContext.xml");
		userMapper = (UserMapper)context.getBean("UserMapper");	
	}

	@Override
	public User findSingleUser(User user) {
		List<User> userList = userMapper.selectUser(user,null,null,null);
		if (userList.size() != 0){
			return userList.get(0);
		}
		else{
			return null;
		}		
	}

	@Override
	public List<User> findUser(int pageNo, String queryStr, User user) {
		return userMapper.selectUser(user,(pageNo-1)*pageItem,pageItem,queryStr);
	}

	@Override
	public Integer getTotalNum(String queryStr, User user) {
		return userMapper.getTotalNum(user, queryStr)/pageItem+1;
	}
	
	@Override
	public boolean updateUser(User user) {
		if(userMapper.updateByPrimaryKeySelective(user)==1)
				return  true;
		else
			return false;
		
	}

	@Override
	public boolean addUser(User user) {
		if(userMapper.insertSelective(user) == 1)
			return true;
		else
			return false;
	}
	
	@Override
	public boolean updateRole(String idStr, int role) {
		if(userMapper.updateRole(idStr,role) != 0)
			return true;
		else
			return false;
	}

	@Override
	public boolean updateStatus(String idStr, int status) {
		if(userMapper.updateStatus(idStr,status) != 0)
			return true;
		else
			return false;
	}
	
	@Override
	public boolean updatePassword(User user) {
		if(userMapper.updatePassword(user)==1)
			return  true;
		else
			return false;
	}
}
